<template>
  <el-card shadow="never" class="aui-card--fill managementWrapper">
    <div class="mod-sys__user">
      <el-form
        :inline="true"
        :model="dataForm"
        @keyup.enter.native="getDataList()"
      >
        <el-form-item>
          <el-input v-model="dataForm.name" placeholder="BOM名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="dataForm.product_name"
            placeholder="产品名称"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <!-- <TreeSelect
            v-model="value"
            :list="classifyList"
            :obj="options"
            tipText="产品分类"
          ></TreeSelect> -->
          <el-cascader
            @change="treeChange"
            v-model="searchIds"
            :options="classifyList"
            :props="{ multiple: true, checkStrictly: true }"
            placeholder="产品分类"
            clearable
          ></el-cascader>
        </el-form-item>
        <!-- <el-form-item>
              <ren-dept-tree v-model="dataForm.deptId" :placeholder="$t('dept.title')" :query="true"></ren-dept-tree>
            </el-form-item> -->
        <el-form-item>
          <el-button @click="getDataList()">搜索</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="toAdd()">创建BOM</el-button>
        </el-form-item>
      </el-form>
      <el-table
        v-loading="dataListLoading"
        :data="dataList"
        border
        @selection-change="dataListSelectionChangeHandle"
        @sort-change="dataListSortChangeHandle"
        style="width: 100%"
      >
        <el-table-column
          type="index"
          label="序号"
          header-align="center"
          align="center"
          width="80"
        ></el-table-column>
        <el-table-column
          prop="bom_serial"
          label="BOM编号"
          header-align="center"
          align="center"
          width="241"
        ></el-table-column>
        <el-table-column
          prop="name"
          label="BOM名称"
          width="180"
          header-align="center"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="deptName"
          label="产品名称"
          header-align="center"
          align="center"
          width="360"
        >
          <template slot-scope="scope">
            <div class="disCenter prodBox">
              <div class="imgBox">
                <img :src="scope.row.master_pic" />
              </div>
              <div class="rightBox">
                <el-tooltip
                  class="item"
                  effect="light"
                  :content="scope.row.product_name"
                  placement="top"
                >
                  <span class="top overTwoHidden">{{
                    scope.row.product_name
                  }}</span>
                </el-tooltip>
                <!-- <span
                  class="top overTwoHidden"
                  :title="scope.row.product_name"
                  >{{ scope.row.product_name }}</span
                > -->
                <span class="bottom overHidden"
                  >产品编号: {{ scope.row.product_serial }}</span
                >
                <!-- <span class="bottom"
                  >产品编号: {{ scope.row.product_serial }}</span
                > -->
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="sku_name"
          label="规格"
          header-align="center"
          align="center"
          width="210"
        ></el-table-column>
        <el-table-column
          prop="classify_name"
          label="产品分类"
          header-align="center"
          align="center"
          width="180"
        ></el-table-column>
        <el-table-column
          prop="versions"
          label="版本"
          header-align="center"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="operator_user"
          label="最后操作人"
          header-align="center"
          align="center"
          width="100"
        ></el-table-column>
        <el-table-column
          prop="operator_time"
          label="最后操作时间"
          sortable="custom"
          header-align="center"
          align="center"
          width="180"
        ></el-table-column>
        <el-table-column
          :label="$t('handle')"
          fixed="right"
          header-align="center"
          align="center"
          width="150"
        >
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="toAdd(scope.row)"
              >编辑</el-button
            >
            <el-button type="text" size="small" @click="toDetail(scope.row)"
              >详情</el-button
            >
            <el-button
              type="text"
              size="small"
              @click="deleteHandle(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="num"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="pageSizeChangeHandle"
        @current-change="pageCurrentChangeHandle"
      >
      </el-pagination>
      <el-dialog :visible.sync="dialogVisible" title="查看">
        <img width="100%" :src="dialogImageUrl" class="avatar" />
      </el-dialog>
    </div>
  </el-card>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
import { addDynamicRoute } from '@/router'
// import TreeSelect from '@/components/tree-select'
import TreeSelect from '@/components/custom-tree-select'
import { getProductClassify } from '@/api'
export default {
  mixins: [mixinViewModule],
  components: {
    TreeSelect,
  },
  data() {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '/bom',
        getDataListIsPage: true,
        deleteURL: '/bom_delete',
        deleteIsBatch: true,
      },
      dataForm: {
        name: '',
        classify_ids: [],
        product_name: '',
      },
      classifyList: [],
      searchIds: [],
    }
  },
  created() {
    this.fetchClassifytList()
  },
  methods: {
    treeChange() {
      let arr = []
      this.searchIds.forEach((item) => {
        arr.push(item[item.length - 1])
      })
      this.dataForm.classify_ids = arr
    },
    // 获取tree
    async fetchClassifytList() {
      const res = await getProductClassify()
      if (res?.status === 1) {
        this.classifyList = res.list
        console.log(this.classifyList)
      }
    },
    toDetail(row) {
      const routeParams = {
        routeName: `${this.$route.name}__${row.id}`,
        title: `${this.$route.meta.title} - 详情`,
        path: 'product/bom-detail',
        params: {
          id: row.id,
        },
      }
      addDynamicRoute(routeParams, this.$router)
    },
    toAdd(row) {
      const des = row ? '编辑' : '新增'
      const type = row ? 'edit' : 'add'
      const routeParams = {
        routeName: `${this.$route.name}__${type}`,
        title: `${this.$route.meta.title} - ${des}`,
        path: 'product/bom-add',
        params: {
          id: row?.id || '',
        },
      }
      addDynamicRoute(routeParams, this.$router)
    },
  },
}
</script>

<style lang="scss" scoped>
.managementWrapper {
  .prodBox {
    padding-left: 6px;
    .imgBox {
      width: 64px;
      height: 64px;
      img {
        width: 100%;
        height: 100%;
      }
    }
    .rightBox {
      margin-left: 14px;
      text-align: left;
      .top {
        max-width: 200px;
        margin-bottom: 6px;
      }
      .bottom {
        display: inline-block;
        max-width: 260px;
        color: $mainColor;
      }
    }
  }
}
</style>

<!-- <style lang="scss" scoped>
  ::v-deep .el-table .cell {
    line-height: 19px !important;
  }
  </style> -->
